var userData = 2;
var unitId = 1;
var searchVal = '';
var page = 1;
let limit = 10;
console.log($('body').height())
let tableHeight = $('body').height() - 210;
let cols = [
	[{
			field: 'PROJECT_NAME',
			title: '型号名称',
			width: 200,
			fixed: "left",
			templet: function(d) {
				return `<div class="name">
				<span class="blue">${d.PROJECT_NAME.slice(0,1)}</span>
				<p>${d.PROJECT_NAME}</p>
			</div>`
			}
		},
		{
			field: 'MONEY_1',
			title: '材料费',
			sort: true,
			minWidth: 160,
			align: 'right',
		},
		{
			field: 'MONEY_2',
			title: '专用费',
			sort: true,
			minWidth: 160,
			align: 'right',
		},
		{
			field: 'MONEY_3',
			title: '外协费',
			sort: true,
			minWidth: 160,
			align: 'right'
		},
		{
			field: 'MONEY_4',
			title: '燃料动力费',
			sort: true,
			minWidth: 160,
			align: 'right'
		},

		{
			field: 'MONEY_5',
			title: '事务费',
			sort: true,
			minWidth: 160,
			align: 'right'
		},
		{
			field: 'MONEY_6',
			title: '固定资产折旧',
			sort: true,
			minWidth: 160,
			align: 'right'
		},
		{
			field: 'MONEY_7',
			title: '管理费',
			sort: true,
			minWidth: 160,
			align: 'right'
		},
		{
			field: 'MONEY_8',
			title: '工资及劳务费',
			minWidth: 160,
			sort: true,
			align: 'right'
		},
		{
			title: '合计',
			field: 'total',
			width: 200,
			sort: true,
			fixed: "right",
			align: "right"
		}
	]
];

function getTableData() {
	var data = [];
	$.ajax({
		type: 'get',
		url: http + 'dxqzq',
		data: {
			page_num: page,
			search: searchVal,
			limit_num: limit
		},
		dataType: "json",
		async: true,
		success: function(res) {
			console.log(res)
			if (res.code == 200) {
				let data = res.data ? res.data : [];
				if (res.data) {
					for (let i = 0; i < data.length; i++) {
						let total = parseFloat(data[i].MONEY_1) + parseFloat(data[i].MONEY_2) + parseFloat(
								data[
									i].MONEY_3) + parseFloat(data[i].MONEY_4) + parseFloat(data[i]
							.MONEY_5) +
							parseFloat(data[i].MONEY_6) + parseFloat(data[i].MONEY_7) + parseFloat(data[i]
								.MONEY_8)
						total = total.toFixed(2)
						data[i].total = formatFigure(total)
						data[i].MONEY_1 = formatFigure(parseFloat(data[i].MONEY_1).toFixed(2))
						data[i].MONEY_2 = formatFigure(parseFloat(data[i].MONEY_2).toFixed(2))
						data[i].MONEY_3 = formatFigure(parseFloat(data[i].MONEY_3).toFixed(2))
						res.data[i].MONEY_4 = formatFigure(parseFloat(data[i].MONEY_4).toFixed(2))
						data[i].MONEY_5 = formatFigure(parseFloat(data[i].MONEY_5).toFixed(2))
						data[i].MONEY_6 = formatFigure(parseFloat(data[i].MONEY_6).toFixed(2))
						data[i].MONEY_7 = formatFigure(parseFloat(data[i].MONEY_7).toFixed(2))
						data[i].MONEY_8 = formatFigure(parseFloat(data[i].MONEY_8).toFixed(2))
					}
				}

				tableDrawing(data) // 数据传到 table组件
				pagesDrawing(res) // 数据传到 分页组件
			} else {
				console.log(res);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
}

function tableDrawing(data) {
	layui.use('table', function() {
		let table = layui.table;
		table.render({
			elem: '#newsList',
			cols: cols,
			data: data, // 数据
			limit: limit, // 每页显示的条数
			height: tableHeight,
		});
	})
}

function pagesDrawing(data) {
	layui.use('laypage', function() {
		let laypage = layui.laypage;
		laypage.render({
			elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
			count: data.record_total, //数据总数，从服务端得到
			theme: '#E8F3FF',
			limit: limit, // 每页条数
			prev: '<em><</em>',
			next: '<em>></em>',
			layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
			jump: function(obj, first) {
				console.log(obj.curr)
				console.log(1111)
				//首次不执行
				if (!first) {
					limit = obj.limit;
					numpage(obj.curr, obj.limit) // 分页点击传参 
				}
			}
		});
	})

};

function numpage(curr, page_count) { // 分页切换-数据请求
	$.ajax({
		type: 'get',
		url: http + 'dxqzq',
		data: {
			page_num: curr,
			search: searchVal,
			limit_num: page_count
		},
		dataType: "json",
		async: true,
		success: function(res) {
			console.log(res)
			if (res.code == 200) {
				let data = res.data;
				for (let i = 0; i < data.length; i++) {
					let total = parseFloat(data[i].MONEY_1) + parseFloat(data[i].MONEY_2) + parseFloat(data[
							i].MONEY_3) + parseFloat(data[i].MONEY_4) + parseFloat(data[i].MONEY_5) +
						parseFloat(data[i].MONEY_6) + parseFloat(data[i].MONEY_7) + parseFloat(data[i]
							.MONEY_8)
					total = total.toFixed(2)
					data[i].total = formatFigure(total)
					data[i].MONEY_1 = formatFigure(parseFloat(data[i].MONEY_1).toFixed(2))
					data[i].MONEY_2 = formatFigure(parseFloat(data[i].MONEY_2).toFixed(2))
					data[i].MONEY_3 = formatFigure(parseFloat(data[i].MONEY_3).toFixed(2))
					res.data[i].MONEY_4 = formatFigure(parseFloat(data[i].MONEY_4).toFixed(2))
					data[i].MONEY_5 = formatFigure(parseFloat(data[i].MONEY_5).toFixed(2))
					data[i].MONEY_6 = formatFigure(parseFloat(data[i].MONEY_6).toFixed(2))
					data[i].MONEY_7 = formatFigure(parseFloat(data[i].MONEY_7).toFixed(2))
					data[i].MONEY_8 = formatFigure(parseFloat(data[i].MONEY_8).toFixed(2))
				}
				tableDrawing(data) // 数据传到 table组件
			} else {
				console.log(res);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
};


$(function() {
	getTableData()


	let isFocus = false;
	$('.wrapper_search .search_box input').focus(function() {
		isFocus = true
	})
	$('.wrapper_search .search_box input').blur(function() {
		isFocus = false
	})
	$(document).keypress(function(event) {
		let key = event.keyCode;
		searchVal = $('.wrapper_search .search_box input').val()
		if (key == '13' && isFocus) {
			getTableData()
		}
	})
	$('.wrapper_search .search_box img').click(function() {
		searchVal = $('.wrapper_search .search_box input').val()
		getTableData()
	})

})
